package com.yfeng.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 定义了常用的几个转换方法
 *
 * @author liuyang
 * @version 0.1
 */
public class MutiDateConverter {
    /**
     * 转换格式
     * 外部可以使用
     */
    public static final String FORMAT_PATTERN_STRING = "yyyy-MM-dd";
    public static final String full_FORMAT_PATTERN_STRING = "yyyy-MM-dd HH:mm:ss";
    private static final Log log = LogFactory.getLog(MutiDateConverter.class);

    private static SimpleDateFormat format = new SimpleDateFormat(FORMAT_PATTERN_STRING);

    /**
     * 得到始日期到结束日期之间的间隔日期
     *
     * @param g1
     * @param g2
     * @return 间隔日期集合，
     *         　　　　　没有间隔日期，返回空list对象
     */
    private static List countDateFromStartToEnd(GregorianCalendar g1, GregorianCalendar g2) {
        ArrayList<Date> list = new ArrayList<Date>();
        GregorianCalendar gc1, gc2;

        if (g2.after(g1)) {
            gc2 = (GregorianCalendar) g2.clone();
            gc1 = (GregorianCalendar) g1.clone();
        } else {
            gc2 = (GregorianCalendar) g1.clone();
            gc1 = (GregorianCalendar) g2.clone();
        }
        //list.add(gc1.getTime());//不包含已经有纪录的日期
        while (gc1.before(gc2)) {
            gc1.add(Calendar.DATE, 1);

            int intday1 = gc1.get(Calendar.DAY_OF_MONTH);
            int intday2 = gc2.get(Calendar.DAY_OF_MONTH);
            if (intday1 == intday2)//不包含当天日期
                list.add(gc1.getTime());
        }
        return list;
    }

    /**
     * 按照默认格式化串形式得到日期间间隔
     *
     * @param start
     * @param end
     * @return
     */
    public static List getDateFromStartToEnd(Date start, Date end) {

        String dateStr = format.format(start);
        String dateEnd = format.format(end);

        Date strdate = null;
        Date endate = null;
        try {
            strdate = format.parse(dateStr);
            endate = format.parse(dateEnd);
        } catch (ParseException e) {
            log.error(e.getMessage(),e);
        }
        GregorianCalendar gc1 = new GregorianCalendar();
        GregorianCalendar gc2 = new GregorianCalendar();
        gc1.setTime(strdate);
        gc2.setTime(endate);
        return countDateFromStartToEnd(gc1, gc2);
    }

    /**
     * 没有格式化的日期统计方式
     * 暂时没有使用
     *
     * @param start
     * @param end
     * @return
     */
    public static List getDateFromStartToEndNoFormat(Date start, Date end) {
        GregorianCalendar gc1 = new GregorianCalendar();
        GregorianCalendar gc2 = new GregorianCalendar();
        gc1.setTime(start);
        gc2.setTime(end);
        return countDateFromStartToEnd(gc1, gc2);
    }

    /**
     * 转换日期成字符串
     *
     * @param date
     * @param formatPattern 希望把日期格式成的样式，如果为空，采用yyyy-mm-dd
     * @return
     */
    public static String convertDateToString(Date date, String formatPattern) {
        if (StringUtils.isNotBlank(formatPattern)) {
            SimpleDateFormat _format = new SimpleDateFormat(formatPattern);
            return _format.format(date);
        }
        return format.format(date);
    }

    /**
     * 转化字符串成日期对象
     *
     * @param dateString 如2007-10-20
     * @return
     */
    public static Date convertStringToDate(String dateString) {
        try {
            if (StringUtils.isNotBlank(dateString)) {
                return format.parse(dateString);
            }
        } catch (ParseException e) {
            //log.error(e.getMessage(),e);
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 按照指定格式格式化日期
     *
     * @param dateString
     * @param formatPattern 希望把日期格式成的样式，如果为空，采用yyyy-mm-dd
     * @return
     */
    public static Date convertStringToDate(String dateString, String formatPattern) {

        if (StringUtils.isNotBlank(formatPattern)) {
            SimpleDateFormat _format = new SimpleDateFormat(formatPattern);
            try {
                return _format.parse(dateString);
            } catch (ParseException e) {
                //log.error(e.getMessage(),e);
                log.error(e.getMessage(), e);
            }
        }
        return convertStringToDate(dateString);
    }

    /**
     * 得到制定日期的月份的起始日期
     * 如输入2008-09-20,返回2008-09-01,2008-09-30
     *
     * @param dateString 只支持两种,字符串或者日期格式
     * @return 2008-10-16-liuyang
     */
    public static Object[] getStartAndEndDateOfMonth(Object dateString) {
        Date thedate = null;
        if (dateString instanceof String)
            thedate = convertStringToDate(dateString.toString());
        else if (dateString instanceof Date)
            thedate = (Date) dateString;
        else
            return null;
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(thedate);
        int start = gc.getActualMinimum(Calendar.DAY_OF_MONTH);
        int end = gc.getActualMaximum(Calendar.DAY_OF_MONTH);
        String startstr = gc.get(Calendar.YEAR) + "-" + gc.get(Calendar.MONTH) + "-" + start;
        String endstr = gc.get(Calendar.YEAR) + "-" + gc.get(Calendar.MONTH) + "-" + end;
        return new Object[]{convertStringToDate(startstr), convertStringToDate(endstr)};
    }

    //-----------------------------------------
    //test code
    public static void main(String[] paras) {
        //full_FORMAT_PATTERN_STRING
        //log.info(convertStringToDate("2008-01-01 10:09:00",full_FORMAT_PATTERN_STRING));
        Object[] dates = getStartAndEndDateOfMonth("Thu Oct 16 2008 18:13:23 GMT 0800");
        for (int i = 0; i < dates.length; i++) {
            log.info("-------------测试返回日期数组-----------"+dates[i]);
    	}
    }
}
